<template>
	<view class="home">
		<view class="lottery-box" id="app">
			<h1 class="title">牛马今天来点啥？🍚🍗</h1>
			<view class="tabList">
				<view class="tabbox">

					<view v-for="(item, index) in tabList" :key="index" class="tab_item"
						:class="activeIndex == index ? 'active' : ''" @click="handletab(index)">{{ item.name }}</view>
				</view>
			</view>
			<view class="lottery">
				<view class="lottery-item">
					<view class="lottery-start">
						<view class="box" @click="startLottery">
							<p><b>吃啥</b></p>
							<p>转一转</p>
						</view>
					</view>
					<ul>
						<li v-for="(item, i) in list" :key="i" :class="i == index ? 'on' : ''">
							<view class="box">
								<p>
									<view class="iconfont " :class="'icon-zp'+(i+1)"></view>
									<!-- <image :src="item.img" alt="" /> -->
								</p>
								<p>{{ item.title }}</p>
							</view>
						</li>
					</ul>
				</view>
			</view>
			<!-- 中奖弹窗 -->
			<view class="mask" v-if="showToast"></view>
			<view class="lottery-alert" v-if="showToast">
				<h1>今天牛马餐</h1>
				<view>
					<!-- <image :src="list[index].img" alt="" /> -->
					<view class="iconfont " :class="'icon-zp'+(index+1)"></view>
				</view>
				<h2>
					{{ list[index].title == "再来一次" ? "" : "今天点个"
          }}{{ list[index].title }}
				</h2>
				<view class="btnsave" @click="showToast = false">冲冲冲</view>
			</view>
			<view class="custom">
				<view class="custom_btn" @click="showModal">自定义</view>
				<view class="custom_btn" @click="resetData">重置</view>
			</view>
			<FollowEdit :visible="visible" @close="visible = false" :dataSource="list" @editFoods="editFoods">
			</FollowEdit>
		</view>
	</view>
</template>

<script>
	import FollowEdit from "../../wxcomponents/FollowEdit/FollowEdit.vue";

	export default {
		components: {
			FollowEdit,
		},
		data() {
			return {
				score: 10, //消耗积分
				list: [{
						title: "阿肥发",
					},
					{
						title: "肉粽",
					},
					{
						title: "炒牛肉面",
					},
					{
						title: "麦当劳",
					},
					{
						title: "豆花",
					},
					{
						title: "肯德基",
					},
					{
						title: "黄焖鸡",
					},
					{
						title: "再来一次",
					},
				], //奖品1-9
				index: -1, // 当前转动到哪个位置，起点位置
				count: 8, // 总共有多少个位置
				timer: 0, // 每次转动定时器
				speed: 200, // 初始转动速度
				times: 0, // 转动次数
				cycle: 50, // 转动基本次数：即至少需要转动多少次再进入抽奖环节
				prize: -1, // 中奖位置
				click: true,
				showToast: false, //显示中奖弹窗
				visible: false,
				customList: [
					
				],
				initList: [{
						title: "阿肥发",
					},
					{
						title: "肉粽",
					},
					{
						title: "炒牛肉面",
					},
					{
						title: "麦当劳",
					},
					{
						title: "豆花",
					},
					{
						title: "肯德基",
					},
					{
						title: "黄焖鸡",
					},
					{
						title: "再来一次",
					},
				],
				activeIndex: 0,
				tabList: [{
						name: "干饭",
						code: 1,
					},
					{
						name: "饮料",
						code: 2,
					},
				],
				drinkList: [{
						title: "喜茶",
					},
					{
						title: "奈雪",
					},
					{
						title: "古茗",
					},
					{
						title: "茶百道",
					},
					{
						title: "茶山派",
					},
					{
						title: "沪上阿姨",
					},
					{
						title: "霸王茶姬",
					},
					{
						title: "再来一次",
					},
				],
			};
		},
		onShareAppMessage(res) {
			return {
				title: '外卖大转盘', // 必填，最长28汉字
				path: '/pages/turntable/index', // 带参数示例
				imageUrl: '/static/images/share_turn.png' // 建议尺寸比例5:4
			}
		},
		mounted() {
			if (
			uni.getStorageSync("foods")&&
				JSON.parse(uni.getStorageSync("foods")) &&
				JSON.parse(uni.getStorageSync("foods")).length != 0
			) {
				this.list = JSON.parse(uni.getStorageSync("foods"));
			}
		},
		methods: {
			startLottery() {
				this.index = -1;
				if (!this.click) {
					return;
				}
				this.startRoll();
			},
			// 开始转动
			startRoll() {
				this.times += 1; // 转动次数
				this.oneRoll(); // 转动过程调用的每一次转动方法，这里是第一次调用初始化
				// 如果当前转动次数达到要求 && 目前转到的位置是中奖位置
				if (this.times > this.cycle + 10 && this.prize === this.index) {
					clearTimeout(this.timer); // 清除转动定时器，停止转动
					this.prize = -1;
					this.times = 0;
					this.speed = 200;
					this.click = true;
					var that = this;
					setTimeout((res) => {
						that.showToast = true;
					}, 500);
				} else {
					if (this.times < this.cycle) {
						this.speed -= 10; // 加快转动速度
					} else if (this.times === this.cycle) {
						const index = parseInt(Math.random() * 8, 0) || 0; // 随机获得一个中奖位置
						this.prize = index; //中奖位置,可由后台返回
						if (this.prize > 7) {
							this.prize = 7;
						}
					} else if (
						this.times > this.cycle + 10 &&
						((this.prize === 0 && this.index === 7) ||
							this.prize === this.index + 1)
					) {
						this.speed += 110;
					} else {
						this.speed += 20;
					}
					if (this.speed < 40) {
						this.speed = 40;
					}
					this.timer = setTimeout(this.startRoll, this.speed);
				}
			},

			// 每一次转动
			oneRoll() {
				let index = this.index; // 当前转动到哪个位置
				const count = this.count; // 总共有多少个位置
				index += 1;
				if (index > count - 1) {
					index = 0;
				}
				this.index = index;
			},
			showModal() {
				this.visible = true;
			},
			editFoods(value) {
				this.list = value;
				console.log(value);
			},
			resetData() {
				this.list = this.activeIndex == 0 ? this.initList : this.drinkList;
				this.index = -1;
				uni.showToast({
					title: "已重置餐食",
				});
				uni.setStorageSync("foods", null);

			},
			handletab(index) {
				this.activeIndex = index;
				this.index = -1;
				if (index == 0) {
					this.list = this.initList;
				}
				if (index == 1) {
					this.list = this.drinkList;
				}
			},
		},
	};
</script>

<style lang="less" scoped="">
	* {
		margin: 0px;
		padding: 0px;
		box-sizing: border-box;
	}

	.home {
		height: 100vh;
		background: #ffdeae;
	}

	body {
		font-size: 14px;
	}

	image {
		border: 0px;
	}

	ul,
	li {
		list-style-type: none;
	}

	.lottery-box {
		overflow: hidden;
	}

	.lottery-box .title {
		text-align: center;
		padding: 50px 0;
		padding-bottom: 20px;
		font-size: 18px;
		color: #333;
		font-weight: bold;
		background: #ffdeae;
	}

	.lottery {
		animation: changeBg 0.5s ease infinite;
		overflow: hidden;
		padding: 5px;
		width: 90%;
		margin: 0 auto;
		background-repeat: no-repeat;
		background-size: 100% 100%;
		border-radius: 10px;
	}

	@keyframes changeBg {
		0% {
			background: #eab270;
		}

		100% {
			background: #eab270;
		}
	}

	.lottery .lottery-item {
		height: 340px;
		position: relative;
		margin-top: 10px;
		margin-left: 10px;
	}

	.lottery .lottery-item ul li {
		width: 30.33333333%;
		position: absolute;
		padding-right: 10px;
	}

	.lottery .lottery-item ul li:nth-child(2) {
		left: 33.33333333%;
	}

	.lottery .lottery-item ul li:nth-child(3) {
		left: 66.66666666%;
	}

	.lottery .lottery-item ul li:nth-child(4) {
		left: 66.66666666%;
		top: 110px;
	}

	.lottery .lottery-item ul li:nth-child(5) {
		left: 66.66666666%;
		top: 220px;
	}

	.lottery .lottery-item ul li:nth-child(6) {
		left: 33.33333333%;
		top: 220px;
	}

	.lottery .lottery-item ul li:nth-child(7) {
		left: 0;
		top: 220px;
	}

	.lottery .lottery-item ul li:nth-child(8) {
		left: 0;
		top: 110px;
	}

	.lottery .lottery-item ul li .box {
		height: 100px;
		position: relative;
		text-align: center;
		overflow: hidden;
		background: #ffdeae;
		background-size: 100% 100%;
	}

	.lottery .lottery-item ul li .box image {
		display: block;
		height: 50px;
		margin: 0 auto;
		margin-top: 10px;
		margin-bottom: 5px;
		width: 60px;
	}

	.lottery .lottery-item ul li .box p {
		color: #000;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		font-size: 14px;
	}

	.lottery .lottery-item ul li.on .box {
		background: #ffc155;
		background-size: 100% 100%;
	}

	.lottery .lottery-item ul li.on .box p {
		color: #fff;
	}

	.lottery .lottery-item .lottery-start {
		position: absolute;
		left: 33.33333333%;
		width: 30.33333333%;
		top: 110px;
		padding-right: 10px;
		background: #eab270;
	}

	.lottery .lottery-item .lottery-start .box {
		height: 100px;
		font-size: 14px;
		color: #000;
		cursor: pointer;
		text-align: center;
		overflow: hidden;
		background: #fff;
		background-size: 100% 100%;
		border-radius: 20px;
	}

	.lottery .lottery-item .lottery-start .box p b {
		font-size: 40px;
		margin-top:23px;
		margin-bottom: 15px;
		line-height: 30px;
		display: block;
	}

	.lottery .lottery-item .lottery-start .box:active {
		opacity: 0.7;
	}

	.lottery .lottery-item .lottery-start .box.gray {
		background-size: 100% 100%;
	}

	.lottery .lottery-item .lottery-start .box.gray p {
		color: #d88c09;
		font-weight: bold;
	}
	.lottery .iconfont{
		font-size: 130rpx;
		padding-top: 10rpx;
	}

	.mask {
		width: 100%;
		height: 100%;
		background: rgba(0, 0, 0, 0.7);
		position: fixed;
		overflow: hidden;
		z-index: 222;
		top: 0;
		left: 0;
	}

	.lottery-alert {
		max-width: 180px;
		text-align: center;
		z-index: 10000;
		border-radius: 10px;
		background: #fff;
		padding: 20px;
		position: fixed;
		left: 0;
		right: 0;
		margin: auto;
		top: 50%;
		transform: translateY(-50%);
		.iconfont{
			font-size: 130rpx;
		}
	}

	.lottery-alert h1 {
		font-size: 18px;
		font-weight: bold;
	}

	.lottery-alert image {
		display: block;
		height: 120px;
		margin: 0 auto;
		width: 100px;
	}

	.lottery-alert h2 {
		font-weight: normal;
		color: #000;
		font-size: 15px;
		padding-top: 15px;
	}

	.lottery-alert p {
		color: #666;
		font-size: 16px;
		padding-top: 5px;
	}

	.lottery-alert .btnsave {
		border-radius: 3px;
		box-shadow: none;
		height: 40px;
		cursor: pointer;
		line-height: 40px;
		color: #fff;
		margin-top: 12px;
		background: #eab270;
		font-size: 16px;
	}

	.custom {
		width: 80%;
		margin: auto;
		display: flex;
		justify-content: space-between;
		margin-top: 20px;

		.custom_btn {
			width: 45%;
			text-align: center;
			background: #d88c09;
			color: #fff;
			padding: 10px 0;
			border-radius: 5px;
		}
	}

	.tabList {
		width: 100%;
		padding-bottom: 10px;
		.tabbox {
			width: 90%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			align-self: center;
			margin: auto;
			margin-bottom: 10px;
			.tab_item {
				width: 50%;
				height: 40px;
				background: #fff;
				border-radius: 5px;
				margin-right: 5px;
				text-align: center;
				font-size: 20px;
				line-height: 40px;
				&.active {
					background: #eab270;
					color: #fff;
				}
			}
		}
	}
</style>